#include"BlockQueue.hpp"
#include"Task.hpp"
std::string opers = "+-*/%";
void* Consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while(true)
    {
        Task t = bq->pop();
        t();
        std::cout << "处理人物: " << t.GetTask() << "运算结果是：" << t.GetRetResult() << "thread id: " << pthread_self() << std::endl;
        sleep(1);
    }
}
void* Productor(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    
    int len = opers.size();
    while(true)
    {   
        int data1 = rand() % 10 + 1;
        usleep(10);
        int data2 = rand() % 10 + 1;
        char op = opers[rand() % len];
        Task T(data1, data2, op);

        bq->push(T);
        std::cout << "生产了一个任务："  << T.GetTask() << "thread id: " << pthread_self() << std::endl;
    }
}
int main()
{
    srand(time(nullptr));
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    pthread_t c[3],p[5];
    for(int i = 0; i < 3; i++)
    {
        pthread_create(c+i, nullptr, Consumer, bq);
    }
    for(int i = 0; i < 5; i++)
    {
        pthread_create(p+i, nullptr, Productor, bq);
    }
    for(int i = 0; i < 3; i++)
    {

        pthread_join(c[i], nullptr);
    }
    for(int i = 0; i < 5; i++)
    {

        pthread_join(p[i], nullptr);
    }
    delete bq;
    return 0;
}